package com.maila.xiaoaoschool.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.api.LoginApi;
import com.maila.xiaoaoschool.net.VolleyInterface;
import com.zhy.autolayout.AutoRelativeLayout;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/10/30.
 */

public class CertificateActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.user_name)
    EditText etuserName;
    @BindView(R.id.arl_name)
    AutoRelativeLayout arlName;
    @BindView(R.id.sex)
    EditText etsex;
    @BindView(R.id.arl_sex)
    AutoRelativeLayout arlSex;
    @BindView(R.id.tv_school)
    TextView tvSchool;
    @BindView(R.id.arl_school)
    AutoRelativeLayout arlSchool;
    @BindView(R.id.tv_college)
    EditText etCollege;
    @BindView(R.id.college_rll)
    AutoRelativeLayout collegeRll;
    @BindView(R.id.tv_identity)
    EditText etIdentity;
    @BindView(R.id.rl_certification)
    AutoRelativeLayout rlCertification;
    @BindView(R.id.tv_school_code)
    EditText etschoolCode;
    @BindView(R.id.ral_school_code)
    AutoRelativeLayout ralSchoolCode;
    @BindView(R.id.btn_add_card_a)
    AutoRelativeLayout btnAddCardA;
    @BindView(R.id.btn_add_card_b)
    AutoRelativeLayout btnAddCardB;
    @BindView(R.id.btn_commit)
    TextView btnCommit;
    private String name;
    private String collagename;
    private String sex;
    private String cardid;
    private String Justimg;
    private String Backimg;
    private String stucode;
    private String adcode;
    boolean nameOk;
    boolean sexOk;
    boolean collageOk;
    boolean schoolOK;
    boolean stucodeOk;
    boolean idOK;
    boolean idJimgOK;
    boolean idBimgOK;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_certificate);
        ButterKnife.bind(this);
        initLinstner();
    }

    private void initLinstner() {
//        姓名
        etuserName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                name = s.toString();
                if (name != null && name.length() > 0) {
                    nameOk = true;
                } else {
                    nameOk = false;
                }
            }
        });
//性别
        etsex.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                sex = s.toString();
                if (sex != null && sex.length() > 0) {
                    sexOk = true;
                } else {
                    sexOk = false;
                }
            }
        });
//学号
        etschoolCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                stucode = s.toString();
                if (stucode != null && stucode.length() > 0) {
                    stucodeOk = true;
                } else {
                    stucodeOk = false;
                }
            }
        });
//学院名称
        etCollege.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                collagename = s.toString();
                if(collagename != null && collagename.length() > 0) {
                    collageOk = true;
                } else {
                    collageOk = false;
                }
            }
        });
//        身份证号
        etIdentity.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                cardid = s.toString();
                if(cardid != null && cardid.length() > 0) {
                    idOK = true;
                } else {
                    idOK = false;
                }
            }
        });
    }

    @OnClick({R.id.white_return_iv, R.id.arl_name, R.id.arl_sex, R.id.arl_school, R.id.college_rll, R.id.rl_certification, R.id.ral_school_code, R.id.btn_add_card_a, R.id.btn_add_card_b, R.id.btn_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.arl_name:
                break;
            case R.id.arl_sex:
                break;
            case R.id.arl_school:
                break;
            case R.id.college_rll:
                break;
            case R.id.rl_certification:
                break;
            case R.id.ral_school_code:
                break;
            case R.id.btn_add_card_a:
                break;
            case R.id.btn_add_card_b:
                break;
            case R.id.btn_commit:
                if(nameOk&&sexOk&&collageOk&&schoolOK&&stucodeOk&&idOK&&idBimgOK&&idJimgOK){
                    new LoginApi(this, new VolleyInterface() {
                        @Override
                        public void onSuccess(String result) {
                            try {
                                JSONObject jsonObject=new JSONObject(result);
                                if(jsonObject.optInt("result")==1){
                                    Toast.makeText(CertificateActivity.this, "认证提交", Toast.LENGTH_SHORT).show();
                                    finish();
                                }else {
                                    Toast.makeText(CertificateActivity.this, jsonObject.optString("message"), Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                        }

                        @Override
                        public void onError(VolleyError error) {

                        }
                    }).set_identity(name, collagename, sex, cardid, Justimg, Backimg, stucode, adcode);
                }else {
                    Toast.makeText(this, "请完善好所有信息", Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }
}
